package com.cskaoyan.service;

import com.cskaoyan.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * @author stone
 * @date 2022/05/27 14:35
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    AccountMapper accountMapper;


    //@Transactional(propagation = Propagation.REQUIRES_NEW,isolation = Isolation.REPEATABLE_READ,timeout = 5)
    //@Transactional(readOnly = true)
    @Transactional(noRollbackFor = ArithmeticException.class)
    @Override
    public void transfer(Integer fromId, Integer destId, Integer money) {

        Integer fromMoney = accountMapper.selectMoneyById(fromId) - money; //转账后的money
        Integer destMoney = accountMapper.selectMoneyById(destId) + money; //转账后的money

        accountMapper.update(fromId, fromMoney);
        int i = 1 / 0;
        accountMapper.update(destId, destMoney);


    }
}
